﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AForge.Controls;
using Sunny.UI;

namespace TestProject
{
    public partial class frmZip : UIPage
    {
        /// <summary>
        /// 压缩文件路径
        /// </summary>
        string _YSfileName = string.Empty;

        /// <summary>
        /// 待解压文件路径
        /// </summary>
        string _JYfileName = string.Empty;

        public frmZip()
        {
            InitializeComponent();
        }

        #region 拖拽文件至文件列表里
        /// <summary>
        /// 文件列表拖放效果
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void lsb_fileList_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
            {
                e.Effect = DragDropEffects.Copy;
            }
        }

        /// <summary>
        /// 文件列表拖放进列表
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void lsb_fileList_DragDrop(object sender, DragEventArgs e)
        {
            string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
            this.lsb_fileList.Items.AddRange(files);
        }

        /// <summary>
        /// 文件列表删除选中项 按Delete删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void lsb_fileList_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Delete)
            {
                var indices = this.lsb_fileList.SelectedIndices;
                for (int i = indices.Count - 1; i >= 0; i--)
                {
                    this.lsb_fileList.Items.RemoveAt(indices[i]);
                }
            }
        }
        #endregion

        #region 压缩文件
        /// <summary>
        /// 选择要压缩的文件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_YS_SelectFile_Click(object sender, EventArgs e)
        {
            try
            {
                //创建OpenFileDialog
                OpenFileDialog opnDlg = new OpenFileDialog();
                //设置对话框标题
                opnDlg.Title = "选择文件";
                //启用帮助按钮
                opnDlg.ShowHelp = true;
                //可选择多个文件
                opnDlg.Multiselect = true;

                //如果选择的结果为打开，则选定文件
                if (opnDlg.ShowDialog() == DialogResult.OK && opnDlg.FileNames != null)
                {
                    string[] _fileName = opnDlg.FileNames;
                    foreach (var item in _fileName)
                    {
                        //追加数据到ListBox
                        lsb_fileList.Items.Add(item);
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 压缩
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_YaSuo_Click(object sender, EventArgs e)
        {
            if (this.lsb_fileList.Items.Count == 0)
            {
                this.slab_Info.Text = "请添加要压缩的文件!";
                return;
            }

            #region 配置文件内容
            saveFileDialog1.Filter = "*.zip|zip file";//设置文件类型
            saveFileDialog1.DefaultExt = ".zip";//默认文件的拓展名
            saveFileDialog1.FileName = "压缩后的文件";//文件名
            if (saveFileDialog1.ShowDialog() != DialogResult.OK)
            {
                return;
            }
            #endregion
            //最终压缩后要保存的路径
            string zipFilePath = this.saveFileDialog1.FileName;

            //文件内容列表
            var fileList = new List<string>();
            foreach (var item in this.lsb_fileList.Items)
            {
                var filePath = item.ToString();
                if (!fileList.Contains(filePath))
                {
                    fileList.Add(filePath);
                }
            }

            //备注
            string remark = this.txt_Remark.Text;
            //压缩密码
            string ys_pwd = this.txt_YSPwd.Text;
            //压缩等级
            int level = txt_level.Value;

            try
            {
                if (SharpZip.CompressFile(fileList, zipFilePath, remark, ys_pwd, level))
                {
                    this.slab_Info.Text = "文件压缩完成...";
                    Process.Start("explorer.exe", "/select, " + zipFilePath);//打开资源管理器并选中文件
                }
                else
                {
                    this.slab_Info.Text = "文件压缩失败...";
                }
            }
            catch (Exception ex)
            {
                this.slab_Info.Text = "异常消息:" + ex.Message;
            }
        }

        /// <summary>
        /// 显示密码
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cxb_YS_ShowPwd_CheckedChanged(object sender, EventArgs e)
        {
            if (cxb_YS_ShowPwd.Checked)
            {
                txt_YSPwd.PasswordChar = default;
            }
            else
            {
                txt_YSPwd.PasswordChar = '*';
            }
        }
        #endregion

        #region 解压文件
        /// <summary>
        /// 选择待解压文件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_JY_SelectFile_Click(object sender, EventArgs e)
        {
            try
            {
                //创建OpenFileDialog
                OpenFileDialog opnDlg = new OpenFileDialog();
                //为图像选择一个筛选器
                opnDlg.Filter = "zip文件|*.zip;*.rar;";                    
                //设置对话框标题
                opnDlg.Title = "选择文件";
                //启用帮助按钮
                opnDlg.ShowHelp = true;
                //不可选择多个文件
                opnDlg.Multiselect = false;

                //如果选择的结果为打开，则选定文件
                if (opnDlg.ShowDialog() == DialogResult.OK && opnDlg.FileName != string.Empty)
                {
                    //读取当前选中的文件地址
                    _JYfileName = opnDlg.FileName;

                    //追加数据到ListBox
                    txt_JieYaFile.Text = _JYfileName;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 显示密码
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cbx_JY_ShowPwd_CheckedChanged(object sender, EventArgs e)
        {
            if (cbx_JY_ShowPwd.Checked)
            {
                txt_JYPwd.PasswordChar = default;
            }
            else
            {
                txt_JYPwd.PasswordChar = '*';
            }
        }

        #endregion

        /// <summary>
        /// 解压文件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_JieYa_Click(object sender, EventArgs e)
        {
            try
            {
                //待解压文件路径
                string zipFile = this.txt_JieYaFile.Text;
                if (!string.IsNullOrEmpty(zipFile))
                {
                    //判断是否为有效路径
                    if (!File.Exists(zipFile))
                    {
                        slab_Info.Text = "要解压的文件路径不正确，请重新选择！";
                    }
                    //判断是否选择文件夹
                    if (folderBrowserDialog1.ShowDialog() != DialogResult.OK)
                    {
                        return;
                    }

                    //解压密码
                    string JY_Pwd = txt_JYPwd.Text;
                    //待解压目录
                    string destFolder = this.folderBrowserDialog1.SelectedPath;
                    if (SharpZip.DecomparessFile(zipFile, destFolder, JY_Pwd))
                    {
                        this.slab_Info.Text = "文件解压成功...";
                        Process.Start("explorer.exe", "/select, " + destFolder);//打开资源管理器并选中文件夹
                    }
                    else
                    {
                        this.slab_Info.Text = "文件解压失败...";
                    }
                }
                else
                {
                    this.slab_Info.Text = "请选择要解压的文件!";
                }
            }
            catch (Exception ex)
            {
                this.slab_Info.Text = "异常消息:" + ex.Message;
            }
        }
    }
}
